package com.liuke.question2;

import com.google.common.collect.Lists;
import com.liuke.question1.pojo.UriBean;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: 刘科
 * @Date: 2021/06/21/14:45
 * @Description:
 */
public class Question {
    private static String FILENAME = "StringUtils.java";

    public Integer countRow() {
        int sumRow = 0;
        List<String> textStr = readFile();
        int start = 0;
        int end = 0;
        while (start < textStr.size() && end < textStr.size()) {
            String row = textStr.get(start);
            if (row.isEmpty()) {
                //跳过空行
                start++;
                continue;
            }
            if (row.charAt(0) == '/') {
                //判断是单行注释还是多行注释
                if (row.charAt(1) == '/') {
                    //单行注释
                    start++;
                    continue;
                } else {
                    // 多行注释
                    end = ++start;
                    while (end < textStr.size()) {
                        String mulRow = textStr.get(end);
                        if (mulRow.length() == 2 && mulRow.equals("*/")) {
                            start = end;
                            break;
                        }
                        end++;
                    }
                }
            } else {
                sumRow++;
            }
            start++;
        }
        return sumRow;
    }


    private List<String> readFile() {
        InputStreamReader read = null;
        List<String> textStr = Lists.newArrayList();
        ArrayList<UriBean> uriBeanArrayList = Lists.newArrayList();
        String filePath = com.liuke.question2.Question.class.getClassLoader().getResource(FILENAME).getPath();
        File file = new File(filePath);
        try {
            read = new InputStreamReader(new FileInputStream(file));
            if (file.isFile() && file.exists()) {
                BufferedReader bufferedReader = new BufferedReader(read);
                String txt = null;
                while ((txt = bufferedReader.readLine()) != null) {
                    textStr.add(txt.trim());
                }
            } else {
                throw new Exception("文件不存在");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (read != null) {
                try {
                    read.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        return textStr;
    }
}
